const router = require('koa-router')()
const path = require('path')
const fs = require('fs')
router.get('/getuser', async function (ctx, next) {
    let sql = "select * from user";
    let result = await ctx.db.EXCUTE(sql);
    for (let i in result) {
        if (result[i].image != null) {
            let header = result[i].image.split('/')[0];
            let background = result[i].background.split('/')[0]
            if (header == '..') {
                let arr1 = result[i].image.split('/');
                let arr2 = arr1.splice(2, 4)
                arr2 = arr2.join('/')
                //绝对地址
                let filePath = path.join(__dirname, '../../../小程序/', arr2)
                let fileObj = fs.readFileSync(filePath);
                let file = fileObj.toString('base64');
                let imgUrl = `data:image/png;base64,${file}`
                result[i].image = imgUrl;
            }
            if (background == '..') {
                let arr1 = result[i].background.split('/');
                let arr2 = arr1.splice(2, 4)
                arr2 = arr2.join('/')
                //绝对地址
                let filePath = path.join(__dirname, '../../../小程序/', arr2)
                let fileObj = fs.readFileSync(filePath);
                let file = fileObj.toString('base64');
                let imgUrl = `data:image/png;base64,${file}`
                result[i].background = imgUrl;
            }
        }
    }
    ctx.body = { data: result, status: "获取用户信息成功" }
})

// 查找用户信息
router.post('/seluser', async function (ctx, next) {
    let userName = ctx.request.body.username;
    let sql = `select * from user where username like '%${userName}%'`;
    let result = await ctx.db.EXCUTE(sql);
    if (result == '') {
        ctx.body = { status: "没有该用户的消息，请重新输入" }
    } else {
        for (let i in result) {
            if (result[i].image != null) {
                let header = result[i].image.split('/')[0];
                let background = result[i].background.split('/')[0]
                if (header == '..') {
                    let arr1 = result[i].image.split('/');
                    let arr2 = arr1.splice(2, 4)
                    arr2 = arr2.join('/')
                    //绝对地址
                    let filePath = path.join(__dirname, '../../../小程序/', arr2)
                    let fileObj = fs.readFileSync(filePath);
                    let file = fileObj.toString('base64');
                    let imgUrl = `data:image/png;base64,${file}`
                    result[i].image = imgUrl;
                }
                if (background == '..') {
                    let arr1 = result[i].background.split('/');
                    let arr2 = arr1.splice(2, 4)
                    arr2 = arr2.join('/')
                    //绝对地址
                    let filePath = path.join(__dirname, '../../../小程序/', arr2)
                    let fileObj = fs.readFileSync(filePath);
                    let file = fileObj.toString('base64');
                    let imgUrl = `data:image/png;base64,${file}`
                    result[i].background = imgUrl;
                }
            }
        }
        ctx.body = { data: result, status: "查找用户信息成功" }
    }
})

module.exports = router

